import json

import requests
import time
import MySQLdb
from requests.exceptions import ProxyError, ConnectionError, Timeout


headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Origin": "http://zjj.zhuzhou.gov.cn",
    "Proxy-Connection": "keep-alive",
    "Referer": "http://zjj.zhuzhou.gov.cn/c13948/?tdsourcetag=s_pctim_aiomsg",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0",
    "X-Requested-With": "XMLHttpRequest"
}
cookies = {
    "HttpOnly": "",
    "JSESSIONID": "7904AF97BFFF705D31A5F5864CE8BAD3"
}
url = "http://zjj.zhuzhou.gov.cn/fcjadpater/select/WWW_YSXK_002"
data = {
    "verifycode": "",
    "cdno": "",
    "lname": "",
    "pageIndex": "1",
    "pageSize": "15"
}

# 配置数据库
db_config = {
    'host': 'localhost',
    'user': 'root',  # 替换为你的用户名
    'password': '',  # 替换为你的密码
    'database': 'bilibili',  # 替换为你的数据库名
    'port': 3307,
    'autocommit': True  # 自动提交事务
}

# 建立数据库连接
conn = MySQLdb.connect(**db_config)
cursor = conn.cursor()
print("数据库连接成功")
page_number = 410  # 从第一页开始
cache_data_list = []
while True:
    try:
        response = requests.post(url, headers=headers, cookies=cookies, data=data, verify=False)

        result = response.json()
        data_list = json.loads(result["rows"])

        if not data_list:  # 如果数据为空，说明没有更多数据了
            print("没有更多数据了")
            has_more_data = False
            break
        data["pageIndex"] = page_number
        # 处理当前页数据
        for item in data_list:
            number = item["cdno"]
            address = item["lname"]
            cache_data_list.append((number, address))
        print(f"第{page_number}页,共{len(cache_data_list)}")
        page_number += 1
        # sql命令
        sql = """INSERT INTO tb_home (number, address)VALUES (%s, %s)"""
        cursor.executemany(sql, cache_data_list)
        conn.commit()
        print(f"第{page_number - 1}页数据已完成插入,已存入数据库{len(cache_data_list)}")
        cache_data_list.clear()
    except MySQLdb.Error as e:
        print(f"数据库错误: {e}")
        break
    except Exception as e:
        print(f"其他错误: {e}")
        time.sleep(10)

# 关闭数据库连接
cursor.close()
conn.close()
print("数据库连接已关闭")
